//
//  MainManager.m
//  CBT
//
//  Created by Mr.Zhang on 2018/11/30.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "MainManager.h"

#import "MainModel.h"

@implementation MainManager

+(void)mainIncomeWithUserID:(NSString *)userID block:(manager_result_block)block
{
    NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
    [param setObject:userID forKey:@"userId"];
    [NetWork requestWithURL:Network_GetIncome params:param httpMethod:@"GET" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
        block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
    } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
        block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
    }];
}

+(void)mainCoinInfoListWithUserID:(NSString *)userID page:(int)page block:(manager_result_block)block
{
    NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
    [param setObject:[CBTLocalUserManager sharedInstance].userId forKey:@"userId"];
    [param setObject:@"20" forKey:@"row"];
    [param setObject:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [NetWork requestWithURL:Network_GetCoinInfo params:param httpMethod:@"GET" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
        block(result,[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
    } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
        block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
    }];
}

+(void)mainGetNewsListWithType:(NSString *)type page:(NSInteger)page block:(manager_result_block)block
{
    
    NSMutableDictionary * param = [[NSMutableDictionary alloc]init];
    [param setObject:type forKey:@"type"];
    [param setObject:@"20" forKey:@"row"];
    [param setObject:@"1" forKey:@"page"];
    [NetWork requestWithURL:Network_GetNews params:param httpMethod:@"GET" finishedBlock:^(NSURLSessionDataTask *task, id  _Nullable result) {
        
        
        NSMutableArray *dataArray = [NSMutableArray array];
        MainNewsModel *mainNewsModel = [MainNewsModel mj_objectWithKeyValues:result[@"data"]];
        if ([type intValue] == 1) {
            for (int i = 0; i < mainNewsModel.newsInfo.count; i++) {
                MainNewsItemModel *model = mainNewsModel.newsInfo[i];
                model.isOpen = YES;
                model.isTopCell = YES;
                model.rightImageViewImageName = @"gonggaoxiangqing_xiala";
                CGFloat titleHeigh = ceilf([NSString sizeWithString:model.title font:[UIFont systemFontOfSize:15] width:ScreenNarrowSide - 60 height:0]);
                titleHeigh = titleHeigh >= 36 ? 36 : 18;
                
                CGFloat detailTitleHeigh = ceilf([NSString sizeWithString:model.content font:[UIFont systemFontOfSize:14] width:ScreenNarrowSide - 60 height:0]);
                
                model.detailLabelHeight = detailTitleHeigh;
                model.cellHeight =  16 + titleHeigh + 13 + detailTitleHeigh + 13 + 9 + 19 + 15;
                model.rootViewHeight = 16 + titleHeigh + 13 + detailTitleHeigh + 13 + 9 + 19;
                [mainNewsModel.textArray addObject:model.title];
            }
        }else{
            for (int i = 0; i < mainNewsModel.newsInfo.count; i++) {
                MainNewsItemModel *itemModel =  mainNewsModel.newsInfo[i];
                itemModel.isOpen = NO;
                itemModel.isTopCell = i == 0;
                itemModel.rightImageViewImageName = @"gonggaoxiangqing_xiala";
                CGFloat titleHeigh = ceilf([NSString sizeWithString:itemModel.title font:[UIFont systemFontOfSize:15] width:ScreenNarrowSide - 60 height:0]);
                titleHeigh = titleHeigh >= 36 ? 36 : 18;
                
                CGFloat detailTitleHeigh = ceilf([NSString sizeWithString:itemModel.content font:[UIFont systemFontOfSize:14] width:ScreenNarrowSide - 60 height:0]);
                detailTitleHeigh = itemModel.isTopCell ? detailTitleHeigh : (detailTitleHeigh >= 34 ? 34 : 17);
                
                itemModel.detailLabelHeight = detailTitleHeigh;
                itemModel.cellHeight =  16 + titleHeigh + 13 + detailTitleHeigh + 13 + 9 + 19 + 15;
                itemModel.rootViewHeight = 16 + titleHeigh + 13 + detailTitleHeigh + 13 + 9 + 19;
                [dataArray addObject:itemModel];
            }
        }
        
        block([type intValue] == 1 ? mainNewsModel : @{@"array":dataArray,@"totalPage":[NSString numberExcludeNullString:mainNewsModel.totalPage]},[result[@"code"]intValue] == 100 ? kHttpRequestResultNormal : kHttpRequestResultError,result[@"message"]);
        
        
    } failureBlock:^(NSURLSessionDataTask *task, NSError *error) {
        block(nil,kHttpRequestResultError,kLocalizedString(@"网络异常，请稍后重试",nil));
    }];   
}
@end
